/*
 * Copyright 2011 CraneLeeon authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.crane.webtemplate.action;

import org.crane.webtemplate.exception.TextMessageException;
import org.crane.webtemplate.model.ImageFile;
import java.io.File;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.hibernate.exception.ConstraintViolationException;
import org.json.JSONObject;
import static org.crane.webtemplate.Tokens.*;

/**
 *
 * @author CraneLeeon
 */
@Action("/upload_image")
@Results({
    @Result(name = "page", type = "dispatcher", location = "upload_image.jsp"),
    @Result(name = "json", type = "dispatcher", location = "/WEB-INF/jsp/json.jsp"),
    @Result(name = "xml", type = "dispatcher", location = "/WEB-INF/jsp/xml.jsp")
})
public class upload_image extends BaseAction {
    
    protected static final Log logger = LogFactory.getLog(upload_image.class);
    
    public static String CHARSET = "UTF-8";
    private File[] filedata;
    private String[] contentTypes;
    private String[] filesNames;

    public void setFiledataContentType(String[] contentTypes) {
        this.contentTypes = contentTypes;
    }

    public void setFiledataFileName(String[] fileNames) {
        this.filesNames = fileNames;
    }

    public void setFiledata(File[] filedata) {
        this.filedata = filedata;
    }
    
    @Override
    public Class getBoClass() {
        return ImageFile.class;
    }
    
    @Override
    public String execute() throws Exception {
        try {
            switch (this.act) {
                case ACT_SAVE:
                    this.save();
                    break;
//                case ACT_DELETE:
//                    this.delete();
//                    break;
                default:
                    this.json = new JSONObject().put("success", false).put("info", "Wrong Request!").toString();
            }
        } catch (TextMessageException ex) {
            this.json = new JSONObject().put("success", false).put("info", (ex.getPrefixMessage() == null ? "" : ex.getPrefixMessage()) + getText(ex.getMessage()) + (ex.getSuffixMessage() == null ? "" : ex.getSuffixMessage())).put("err_code", ex.getErrCode()).toString();
        } catch (ConstraintViolationException ex) {
            logger.debug(ex);
            this.json = new JSONObject().put("success", false).put("info", getText("ConstraintViolationException")).toString();
        } catch (Exception ex) {
            logger.debug(ex);
            this.json = new JSONObject().put("success", false).put("info", ex).toString();
        }
        return "json";
    }
    
    @Override
    protected void save() throws Exception {
       
    }

}
